﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO.Ports;
using Incendios0_1.Camera.Pelco;
using System.Configuration;
using Incendios0_1.Camera.media.filters;
using Incendios0_1.Camera.wpfdatamanager;
using AsyncCamera.Camera.Pelco;
using Incendios0_1.tools;

namespace Incendios0_1.Camera
{
    /// <summary>
    /// Interaction logic for CameraManualControlW.xaml
    /// </summary>
    public partial class CameraManualControlW : Window
    {
        private CameraSink dcc;
        public PelcoCamera camera;

        public CameraManualControlW(PelcoCamera camera)
        {
            InitializeComponent();
            dcc = new CameraSink();
            camera.PlugIn(dcc.sink);
            dcc.Height = videoPanel.Height;
            dcc.Width = videoPanel.Width;
            dcc.Margin = new Thickness(0, 0, 0, 0);
            videoPanel.Children.Add(dcc);
            mainGrid.DataContext = camera.cameraData;
            this.camera = camera;
        }

        #region Panel numerico para Preset
        private void button14_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "1";
            /*Tuple<decimal,decimal> coords = camera.readCoords();
            Console.Write(coords.Item1);
             * camera.MoveRight(4);*/
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "2";
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "3";
        }

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "4";
        }

        private void button18_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "5";
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "6";
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "7";
        }

        private void button21_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "8";
        }

        private void button22_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "9";
        }

        private void button23_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text += "0";
        }
        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!SerialPortManager.CameraPortOpen)
            {
                string messageBoxText = "El puerto de control de cámaras no existe, la ventana de control se cerrará";
                string caption = "Puerto no Encontrado";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Error;
                MessageBox.Show(messageBoxText, caption, button, icon);
                this.Close();
            }
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            camera.Wipe();
        }

        private void buttonup_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }
        private void buttonup_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.MoveUp();
        }





        private void buttondown_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.MoveDown();
        }

        private void buttondown_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }


        private void buttonright_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.MoveRight();
        }

        private void buttonright_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttonleft_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.MoveLeft();
        }

        private void buttonleft_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttonzoomalejar_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.ZoomOut();
        }

        private void buttonzoomalejar_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttonzoomacercar_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.ZoomIn();
        }

        private void buttonzoomacercar_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttondiafclose_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.CloseIris();
        }

        private void buttondiafclose_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttondiafopen_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.OpenIris();
        }

        private void buttondiafopen_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttonfocusnear_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.FocusNear();
        }

        private void buttonfocusnear_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttonfar_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            camera.MotionStop();
        }

        private void buttonfar_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            camera.FocusFar();
        }

        private void buttonset_Click(object sender, RoutedEventArgs e)
        {
            camera.SetPreset();
            /*if (textpreset.Text == "")
            { }
            else
                portcamaras.Write(controlcamara.computeByte_setPreset(address, Convert.ToInt32(textpreset.Text.ToString())), 0, 7);*/
        }

        private void buttoncall_Click(object sender, RoutedEventArgs e)
        {
            camera.CallPreset();
           /* if (textpreset.Text == "")
            { }
            else
                portcamaras.Write(controlcamara.computeByte_callPreset(address, Convert.ToInt32(textpreset.Text.ToString())), 0, 7);*/
        }

        private void buttonclear_Click(object sender, RoutedEventArgs e)
        {
            camera.ClearPreset();
            textpreset.Text = "";
           /* if (textpreset.Text == "")
            { }
            else
                portcamaras.Write(controlcamara.computeByte_clearPreset(address, Convert.ToInt32(textpreset.Text.ToString())), 0, 7);*/
        }

        private void buttonmenu_Click(object sender, RoutedEventArgs e)
        {
            camera.Menu();
        }


        private void buttonturbo_Click(object sender, RoutedEventArgs e)
        {
            camera.cameraData.turbo = !camera.cameraData.turbo;
            if (camera.cameraData.turbo)
            {
                buttonturbo.Content = "on";
            }
            else
            {
                buttonturbo.Content = "off";
            }
        }

        private void buttonsupr_Click(object sender, RoutedEventArgs e)
        {
            textpreset.Text = "0";
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            camera.Reset();
            
        }
    }
}
